"""
内建函数map和reduce
- map(f, iterable): f接受一个参数，依次作用于iterable的元素
- reduce(f, iterable): f接受两个参数，将f返回的结果和iterable的下一个元素进行运算
- filter(f, iterable): f接受一个参数，返回True的元素组成的列表
- sorted(iterable, key=None): 对iterable进行排序，key指定排序的规则
"""

from functools import reduce
def f(x):
  return x * x

print(list(map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

print(list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

def add(x, y):
  return x + y

print(reduce(add, [1, 2, 3, 4, 5, 6, 7, 8, 9]))

def fn(x,y):
  return x * 10 + y

print(reduce(fn, [1, 2, 3, 4, 5]))

def is_odd(x):
  return x % 2 == 1

print(list(filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

print(list(sorted([36, 5, -12, 9, -21])))
print(list(sorted([36, 5, -12, 9, -21], key=abs)))